package com.example.manpower.Intercepter;

import com.example.manpower.Util.RedisUtil;
import com.example.manpower.Util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        String uri = request.getRequestURI();

        if ("/Admin/login".equals(uri) || "/User/export".equals(uri) || "/User/import".equals(uri) || "OPTIONS".equals(request.getMethod())) {
            System.out.println("测试一");
            return true;
        }else {
            if (token == null) {
                System.out.println("拦截1");
                return false;
            }
            TokenUtil tokenUtil = new TokenUtil();
            if (tokenUtil.verifyToken(token) && redisUtil.hasKey(token)) {
                return true;
            }
        }
        System.out.println("拦截2");
        return false;
    }

}